package demo;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {
        Client client = new Client();

        try {
            client.fun3();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void fun1() throws IOException {
        Socket socket = new Socket("localhost",8080);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("你好我是客户端".getBytes());
        outputStream.close();
        socket.close();
    }

    private void fun2() throws IOException {
        Socket socket = new Socket("localhost",8080);
        OutputStream outputStream = socket.getOutputStream();
        Scanner scanner = new Scanner(System.in );
        while (true){
            System.out.print("我:");
            String str = scanner.next();
            outputStream.write((str+"#").getBytes());
            if ("886".equals(str)){
                break;
            }
        }
        socket.close();
    }

    private void fun3() throws IOException {

        Socket socket = new Socket("localhost",8080);
        OutputStream outputStream = socket.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream("demo21\\src\\1.jpg");
        byte[] buf = new byte[1024];
        int len;
        while ((len = fileInputStream.read()) != -1){
            String str = new String(buf,0 ,len);
            outputStream.write(str.getBytes());
        }
        socket.shutdownInput();

        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        System.out.println(bufferedReader.readLine());

        inputStream.close();
        fileInputStream.close();
        outputStream.close();
        socket.close();

    }
}
